<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-if="demoList.length < 1"
          >新增</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="demoList">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="标题" align="center" prop="name" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改单设计器保存demo对话框 -->
    <el-dialog
      :visible.sync="open"
      append-to-body
      :close-on-press-escape="false"
      width="1800px"
      :close-on-click-modal="false"
      :show-close="false"
      class="custom-dialog"
      top="0vh"
    >
      <div class="el-dialog-div">
        <form-designer ref="formDesigner" v-model="form.jsonData"></form-designer>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm"
          >确 定</el-button
        >
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDemo, getDemo, delDemo, addDemo, updateDemo } from "@/api/designer/demo";
export default {
  name: "Demo",
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 单设计器保存demo表格数据
      demoList: [],
      // 弹出层标题
      title: "设计模板",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {
        name: "表单设计器demo",
        jsonData: "",
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询单设计器保存demo列表 */
    getList() {
      this.loading = true;
      listDemo(this.queryParams).then((response) => {
        this.demoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
    },
    submitForm() {
      this.$modal.loading("正在保存，请稍候...");
      addDemo(this.form)
        .then((res) => {
          this.open = false;
          const data = this.$refs.formDesigner.getFormData();
          this.form.jsonData = data;
          this.reset();
          this.getList();
        })
        .finally(() => {
          this.$modal.closeLoading();
        });
    },
    handleDelete(row) {
      delDemo(row.id).then((res) => {
        this.$modal.msgSuccess("删除成功");
        this.getList();
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        name: undefined,
        jsonData: "{}",
      };
    },
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids;
      getDemo(id).then((response) => {
        this.loading = false;
        this.form = response.data;
        this.open = true;
        this.title = "修改单设计器保存demo";
      });
    },
  },
  watch: {
    form: {
      handler(newVal) {

      },
      immediate: true,
      deep: true,
    },
  },
};
</script>
<style scoped>
.el-dialog-div {
  height: 60vh;
  overflow-x: hidden;
}
</style>
